package cn.szzsi.service;

import cn.szzsi.common.model.Orders;
import cn.szzsi.common.model.zshop.AgentBalanceDetailService;
import cn.szzsi.common.model.zshop.AgentBalanceService;
import cn.szzsi.common.model.zshop.Member;
import cn.szzsi.common.model.zshop.PlateformConfig;
import com.jfinal.kit.StrKit;
import org.apache.xpath.operations.Or;

import java.math.BigDecimal;

/**
 * 商户收益-->给代理商返现
 */
public class AgentRecommendBusinessService {
    public static final AgentRecommendBusinessService service=new AgentRecommendBusinessService();

    /**
     * 商户收益,给推荐代理商返现
     * @param order 订单
     */
    public void orderBackMoneyToAgent(Orders order){
        PlateformConfig plateformConfig=PlateformConfig.dao.findById(1);
        BigDecimal totalMoney=order.getTotalAmount();//订单总金额
        BigDecimal firstBusinessRecommendPercent=plateformConfig.getFirstBusinessRecommendPercent().divide(new BigDecimal(100));
        BigDecimal secondBusinessRecommendPercent=plateformConfig.getSecondBusinessRecommendPercent().divide(new BigDecimal(100));
        BigDecimal thirdBusinessRecommendPercent=plateformConfig.getThirdBusinessRecommendPercent().divide(new BigDecimal(100));

        //1.给直荐推荐的代理商返现
        //(1.)查询该笔订单是哪个商户的
        Member business=Member.dao.getByPhone(order.getUid());
        String firstAgentPid=business.getPid();//直接推荐人
        if(StrKit.notBlank(firstAgentPid)&& !"0".equals(firstAgentPid)){
            Member firstMember=Member.dao.findById(firstAgentPid);
            if(firstMember!=null){
                if(2==firstMember.getAgentStates()) {//满足审核通过
                    String firstMemberId=firstMember.getId();
                    BigDecimal firstAgentRecommendMoney=totalMoney.multiply(firstBusinessRecommendPercent);//直荐人返现
                    AgentBalanceService.service.updateBalance(firstMemberId,firstAgentRecommendMoney);//更新余额
                    AgentBalanceDetailService.service.saveInfo(firstMemberId,firstAgentRecommendMoney,2,"",business.getId(),order.getOrderSn(),1,"代理商直荐商户收益");

                }

                //直属(一级)代理商推荐商户收益开始
                String secondAgentPid=firstMember.getPid();
                if(StrKit.notBlank(secondAgentPid) && !"0".equals(secondAgentPid)){
                    Member secondMember=Member.dao.findById(secondAgentPid);
                    if (secondMember!=null){
                        if(2==secondMember.getAgentStates()&& secondMember.getAgentLevel()>=2){//审核通过,等级大于返佣等级
                            String secondMemberId=secondMember.getId();
                            BigDecimal secondAgentRecommendMoney=totalMoney.multiply(secondBusinessRecommendPercent);//直属(一级)代理商推荐商户收益
                            AgentBalanceService.service.updateBalance(secondMemberId,secondAgentRecommendMoney);//更新余额
                            AgentBalanceDetailService.service.saveInfo(secondMemberId,secondAgentRecommendMoney,2,firstAgentPid,business.getId(),order.getOrderSn(),2,"直属(一级)代理商推荐商户收益");

                        }
                    }
                    //直属(一级)代理商推荐商户收益结束

                    String thirdAgentPid=secondMember.getPid();
                    if(StrKit.notBlank(thirdAgentPid) && !"0".equals(thirdAgentPid)){
                        Member thirdMember=Member.dao.findById(thirdAgentPid);
                        if(thirdMember!=null){
                            if(2==thirdMember.getAgentStates() && thirdMember.getAgentLevel()>=3){
                                String thirdMemberId=thirdMember.getId();
                                BigDecimal thirdAgentRecommendMoney=totalMoney.multiply(thirdBusinessRecommendPercent);//直属(二级)代理商推荐商户收益
                                AgentBalanceService.service.updateBalance(thirdMemberId,thirdAgentRecommendMoney);//更新余额
                                AgentBalanceDetailService.service.saveInfo(thirdMemberId,thirdAgentRecommendMoney,2,secondAgentPid,business.getId(),order.getOrderSn(),3,"直属(二级)代理商推荐商户收益");
                            }
                        }
                    }
                }
            }
        }

    }
}
